<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/1/21
 * Time: 14:55
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Area extends Base
{
    /**
     * 省
     * @return mixed|\think\response\Json
     */
    public function sheng(){
        if(request()->isAjax ()){
            $district = input('district');
            
            $where['level'] = 1;
            if(isset($district) && $district != ''){
                $where['district'] = ['like', '%' . $district . '%'];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('area')->where($where)->count();// 获取总条数
            $lists = Db::name('area')
                ->field('district_id, district')
                ->where($where)
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加市
     * @return mixed|\think\response\Json
     */
    public function addShi(){
        $district_id = input('district_id');
        if(Request::instance()->isPost()){
            $district = trim(input('district'));
            $insert = [
                'district' => $district,
                'pid' => $district_id,
                'level' => 2
            ];
            $res = Db::table('up_area')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('area/addshi', ['district_id' => $district_id]);
    }
    
    /**
     * 市
     * @return mixed|\think\response\Json
     */
    public function shi(){
        if(request()->isAjax ()){
            $district = input('district');
            
            $where['ua1.level'] = 2;
            if(isset($district) && $district != ''){
                $where['ua1.district'] = ['like', '%' . $district . '%'];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('area')->alias('ua1')
                ->join('up_area ua2', 'ua1.pid = ua2.district_id')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('area')->alias('ua1')
                ->join('up_area ua2', 'ua1.pid = ua2.district_id')
                ->field('ua1.district_id, ua1.district, ua2.district pdistrict')
                ->where($where)
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加区
     * @return mixed|\think\response\Json
     */
    public function addQu(){
        $district_id = input('district_id');
        if(Request::instance()->isPost()){
            $district = trim(input('district'));
            $insert = [
                'district' => $district,
                'pid' => $district_id,
                'level' => 3
            ];
            $res = Db::table('up_area')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('area/addqu', ['district_id' => $district_id]);
    }
    
    /**
     * 区
     * @return mixed|\think\response\Json
     */
    public function qu(){
        if(request()->isAjax ()){
            $district = input('district');
            
            $where['ua1.level'] = 3;
            if(isset($district) && $district != ''){
                $where['ua1.district'] = ['like', '%' . $district . '%'];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('area')->alias('ua1')
                ->join('up_area ua2', 'ua1.pid = ua2.district_id')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('area')->alias('ua1')
                ->join('up_area ua2', 'ua1.pid = ua2.district_id')
                ->field('ua1.district_id, ua1.district, ua2.district pdistrict')
                ->where($where)
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加街道
     * @return mixed|\think\response\Json
     */
    public function addStreet(){
        $district_id = input('district_id');
        if(Request::instance()->isPost()){
            $district = trim(input('district'));
            $insert = [
                'district' => $district,
                'pid' => $district_id,
                'level' => 4
            ];
            $res = Db::table('up_area')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('area/addstreet', ['district_id' => $district_id]);
    }
    
    /**
     * 街道
     * @return mixed|\think\response\Json
     */
    public function street(){
        if(request()->isAjax ()){
            $district = input('district');
            
            $where['ua1.level'] = 4;
            if(isset($district) && $district != ''){
                $where['ua1.district'] = ['like', '%' . $district . '%'];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('area')->alias('ua1')
                ->join('up_area ua2', 'ua1.pid = ua2.district_id')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('area')->alias('ua1')
                ->join('up_area ua2', 'ua1.pid = ua2.district_id')
                ->field('ua1.district_id, ua1.district, ua2.district pdistrict')
                ->where($where)
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
}